/**
* ...
* @author Default
* @version 0.1
*/

package bdz.projects.slab.renderers {
	
	import as3.common.ui.menu.core.MenuItem;
	import bdz.common.model.AppEnvironment;
	import as3.projects.slfi.data.ExhibitVO;
	import bdz.dms.vo.ContentVO;
	import flash.display.MovieClip;
	import flash.text.TextFormat;
	import flash.events.Event;
	import as3.common.utils.BitmapUtils;
	import as3.common.display.bdzImage;
	import bdz.common.utils.TweenFilterLite;
	import bdz.common.utils.TweenLite;
	import flash.events.ProgressEvent;
	import flash.text.*;
	import flash.utils.*;
	import fl.transitions.easing.*;
	public class MediaRenderer extends MenuItem{
		
		private var settings:AppEnvironment;
		private var dp:ContentVO;
		private var virtualWidth:Number = 20;
		protected var box:MovieClip;
		protected var imageDisplay:bdzImage;
		protected var masker:MovieClip;
		protected var iloc:String = "path";
		protected var altDP:ExhibitVO;
		protected var defY:Number = 0;
		private var pbg:MovieClip;
		private var pretext:TextField;
		private var dbg:MovieClip;
		public var mycol:Number;
		protected var showdbg:Boolean = true;
		function MediaRenderer(tstyle:Object,frm:TextFormat,sv:String=null){
			super(tstyle, frm, sv);
			
			settings = AppEnvironment.getEnv();
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function ptextFormat():TextFormat{
			var format:TextFormat = new TextFormat();
			var fontObj:Object = getDefinitionByName("pixelate");
			format.color = 0x000000;
			format.font = new fontObj().fontName;
			format.size = 10;
			format.align = "center";
			return format;
		}
		private function setUpField(field:TextField):TextField{
			field.autoSize = "left";
			field.antiAliasType = AntiAliasType.NORMAL;
			field.gridFitType = GridFitType.PIXEL;
			field.embedFonts = true;
			field.selectable = false;
			return field;
		}
		private function createPreloaderText():void{
			pbg = new MovieClip();
			BitmapUtils.createBounds(pbg, Math.ceil(dp.getProperty("owidth")), Math.ceil(dp.getProperty("oheight")), 0, 0x000000);
			addChild(pbg);
			pretext = new TextField();
			setUpField(pretext);
			pretext.setTextFormat(ptextFormat());
			pretext.defaultTextFormat = ptextFormat();
			pretext.text = "RETRIEVING IMAGE";
			pretext.x = pbg.width/2-pretext.width/2;
			pretext.y = pbg.height/2-pretext.height/2;
			addChild(pretext);
		}
		override protected function createField():void{
			
			
		}
		private function imagePreloading(evt:ProgressEvent):void{
			var tot:Number = Math.ceil((evt.bytesLoaded/evt.bytesTotal));
			TweenLite.to(box, .6, {scaleX:tot});
		}
		protected function initializeComponent(evt:Event):void{
			
			
			box = new MovieClip();
			BitmapUtils.createBounds(box, Math.ceil(dp.getProperty("owidth")), Math.ceil(dp.getProperty("oheight")), 1, 0xE5E5E5);
			box.scaleX = 0;
			addChild(box);
			masker = new MovieClip();
			BitmapUtils.createBounds(masker, Math.ceil(dp.getProperty("owidth")), Math.ceil(dp.getProperty("oheight")), 0);
			addChild(masker);
			masker.scaleX = 0;
			
			imageDisplay = new bdzImage();
			imageDisplay.addEventListener(ProgressEvent.PROGRESS, imagePreloading);
			addChild(imageDisplay);
			createPreloaderText();
			
			imageDisplay.source = settings.getSetting("baseURL") + dp.getProperty("image");
			imageDisplay.addEventListener(Event.COMPLETE, showImage);
			imageDisplay.mask = masker;
			imageDisplay.y = masker.y = box.y = defY;
			if (showdbg) {
				dbg = new MovieClip();
				BitmapUtils.createBounds(dbg, Math.ceil(dp.getProperty("owidth")), Math.ceil(dp.getProperty("oheight")), .8, 0x000000);
				addChild(dbg);
				dbg.alpha = 0;
			}
			if(mycol){
				deactivateFocus();
			}
			
		}
		override public function activateFocus():void{
			try{
				TweenLite.to(dbg, .6, {alpha:0});
			}catch(e:Error){
			
			}
			//TweenLite.to(imageDisplay, .6, {alpha:1});
			TweenFilterLite.to(imageDisplay, .6, {type:"color", saturation:1, amount:1, alpha:1})
		}
		override public function deactivateFocus():void{
			try{
				TweenLite.to(dbg, .6, {alpha:1});
			}catch(e:Error){
				
			}
			//TweenLite.to(imageDisplay, .6, {alpha:.9});
			TweenFilterLite.to(imageDisplay, .6, {type:"color", saturation:0, amount:1, alpha:.9})
		}
		protected function animateMask():void {
			
			TweenLite.to(masker, .6, {scaleX:1, ease:Strong.easeInOut});
			TweenLite.to(box, .6, {alpha:0});
			
		}
		protected function showImage(evt:Event):void{
			//trace("showing :: " + imageDisplay.source);
			try{
				removeChild(pbg);
				removeChild(pretext);
			}catch (e:Error) {
				
			}
			TweenLite.to(box, .6, {scaleX:1, ease:Strong.easeInOut, onComplete:animateMask});
		}
		override public function get fieldWidth():Number{
			return Math.ceil(Number(dp.getProperty("owidth")));
		}
		override public function get fieldHeight():Number{
			return 10;
		}
		override public function set info(inf:*):void{
			dp = inf;
		}
		override public function get info():*{
			return dp;
		}
	}
	
}